package cn.edu.nuc.ssm.service.impl;

import cn.edu.nuc.ssm.dao.BbsReplyMapper;
import cn.edu.nuc.ssm.dto.BbsReplyAndUsername;
import cn.edu.nuc.ssm.dto.BbsReplyFirstAndUsername;
import cn.edu.nuc.ssm.dto.SecondBbsReblyAndRAndToUsername;
import cn.edu.nuc.ssm.entity.BbsReply;
import cn.edu.nuc.ssm.service.BbsRelpyService;
import cn.edu.nuc.ssm.util.PageControl;
import cn.edu.nuc.ssm.util.Pager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by Administrator on 2017/4/20.
 */
@Service
public class BbsRelpyServiceImpl implements BbsRelpyService{

    private Logger logger = LoggerFactory.getLogger(BbsRelpyService.class);

    @Autowired
    private BbsReplyMapper bbsReplyMapper;
    @Override
    @Transactional(timeout = 30)
    public int insert(BbsReply record) {

        return bbsReplyMapper.insert(record);
    }


    @Override
    public Pager findPageList(int currentPage,int uid,int sid,int tid,int level) {

        PageControl pageControl = new PageControl();
        pageControl.setCurrentIndex(currentPage);

        Pager<BbsReplyFirstAndUsername> pager = new Pager<>();

        int count = bbsReplyMapper.countReply(uid, sid, tid, level);
        pageControl.setSumCount(count);

        int start = (pageControl.getCurrentIndex()-1) * pageControl.getPagesize();

        List<BbsReplyFirstAndUsername> list = bbsReplyMapper.findPageList(uid,sid,tid,level,start,pageControl.getPagesize());

        pager.setList(list);

        pageControl = pager.detailPage(pageControl);

        logger.debug("pageControll  {}",pageControl);

        pager.setPageControl(pageControl);

        logger.debug("pager  {}",pager);


        return pager;
    }

    @Override
    public BbsReplyAndUsername findbyIdandFirstlevel(Integer id) {
        return bbsReplyMapper.findbyIdandlevel(id,1);
    }

    @Override
    @Transactional(timeout = 30)
    public int insertBbsReply2Level(BbsReply record) {

        record.setLevel(2);

        return bbsReplyMapper.insert(record);
    }

    @Override
    public List<SecondBbsReblyAndRAndToUsername> findbytidandlevel(Integer tid) {

        return bbsReplyMapper.findbytidAnd2level(tid);
    }

    @Override
    public void deletebyIdOrPid(Integer id) {

        bbsReplyMapper.deletebyidorpid(id,id);

    }

    @Override
    public List<BbsReply> find() {

        return bbsReplyMapper.find();
    }
}
